// src/app/workspace/[workspaceId]/page.tsx
'use client';

import {useWorkspaceId} from "@/hooks/social/use-workspace-id";
import {useRouter} from "next/navigation";
import {useCreateChannelModal} from "@/features/channels/store/use-create-channel-modal";
import {useGetWorkspace} from "@/features/workspaces/api/use-get-workspace";
import {useEffect, useMemo, useState} from "react";
import {useGetChannels} from "@/features/channels/api/use-get-channels";
import {Channel, Member, Workspace} from "@/lib/social/types";
import {Loader, TriangleAlert} from "lucide-react";
import {useCurrentMember} from "@/features/workspaces/api/use-current-member";
import {useCurrentUser} from "@/hooks/use-current-user";

// 强制每个请求动态渲染
export const dynamic = 'force-dynamic'

const Page = () => {
        const user = useCurrentUser()

        const workspaceId = useWorkspaceId();
        const router = useRouter()
        const [open, setOpen] = useCreateChannelModal();

        const [workspace, setWorkspace] = useState<Workspace>();
        const [channels, setChannels] = useState<Channel[]>();
        const [member, setMember] = useState<Member>();

        const [w_loading, setW_loading] = useState(false);
        const [c_loading, setC_loading] = useState(false);
        const [m_loading, setM_loading] = useState(false);

        const channelId = useMemo(() => channels?.[0]?.id, [channels]);
        const isAdmin = useMemo(() => member?.role === 'admin', [member?.role]);

        useEffect(() => {
            (async () => {
                const {data: w, isLoading: wLoading} =
                    await useGetWorkspace({id: workspaceId as string});
                setW_loading(wLoading)

                const {data: c, isLoading: cLoading} =
                    await useGetChannels({workspaceId: workspaceId as string})
                setC_loading(cLoading)

                const {data: m, isLoading: mLoading} =
                    await useCurrentMember({
                        workspaceId, userId: user?.id!
                    })
                setM_loading(mLoading)

                setWorkspace(w)
                setChannels(c)
                setMember(m)

                console.log('workspace id page',w, c, m)
            })()
        }, [
            // member,
            m_loading,
            isAdmin,
            // channelId,
            c_loading,
            // workspace,
            w_loading,
            workspaceId,
            open,
            // setOpen,
            router,
        ]);


        if (w_loading || c_loading || m_loading) return <div className={`h-full flex-1 flex items-center
            justify-center flex-col gap-2`}>
            <Loader className={`size-5 animate-spin text-muted-foreground`}/>
        </div>

        if ((!workspace || !member) && !(w_loading || c_loading || m_loading)) return (
            <div className={`h-full flex-1 flex items-center
            justify-center flex-col gap-2`}>
                <TriangleAlert className={`size-5`}/>
                <span className={`text-sm text-muted-foreground`}>Workspace not found</span>
            </div>
        )

        if (channelId) {
            router.push(`/social/workspace/${workspaceId}/channel/${channelId}`);
            // } else if (!open && isAdmin) {
        } else if (!open && isAdmin) {
            setOpen(true)
        }

        // 非管理员无法修改频道 且 当前workspace无频道
        return <div className={`h-full flex-1 flex items-center
            justify-center flex-col gap-2`}>
            <TriangleAlert className={`size-6 text-muted-foreground`}/>
            <span className={`text-sm text-muted-foreground`}>Not channel found</span>
        </div>
    }
;

export default Page;